package com.ymatou.iapi.reachpromotion.service;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.ymatou.iapi.reachpromotion.parameter.Role;
import com.ymatou.iapi.reachpromotion.parameter.UserInfo;

public class ReachPromitionVerify2 {
	/**
	 * 对比两个数据是否内容相同
	 * 
	 * @param object1,object2
	 * @return boolean类型
	 */
	public static boolean compareTwo(Object object1, Object object2) {

		if (object1 == null && object2 == null) {
			return true;
		}
		if (object1 == null && object2 != null) {
			return false;
		}
		if (object1.equals(object2)) {
			return true;
		}
		return false;
	}

	/**
	 * 获取两个对象同名属性内容不相同的列表
	 * 
	 * @param class1
	 *            对象1
	 * @param class2
	 *            对象2
	 * @return
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 */
	public static List<Map<String, Object>> compareTwoClass(Object class1, Object class2,String comment)
			throws ClassNotFoundException, IllegalAccessException {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		// 获取对象的class
		Class<?> clazz1 = class1.getClass();
		Class<?> clazz2 = class2.getClass();
		// 获取对象的属性列表
		Field[] field1 = clazz1.getDeclaredFields();
		Field[] field2 = clazz2.getDeclaredFields();
		// 遍历属性列表field1
		for (int i = 0; i < field1.length; i++) {
			// 遍历属性列表field2
			for (int j = 0; j < field2.length; j++) {
				// 如果field1[i]属性名与field2[j]属性名内容相同
				if (field1[i].getName().equals(field2[j].getName())) {

					field1[i].setAccessible(true);
					field2[j].setAccessible(true);
					
					// 如果field1[i]属性值与field2[j]属性值内容不相同
					if (!compareTwo(field1[i].get(class1), field2[j].get(class2))) {
						Map<String, Object> map2 = new HashMap<String, Object>();
						map2.put("name", comment+"->"+field1[i].getName());
						map2.put("old", field1[i].get(class1));
						map2.put("new", field2[j].get(class2));
						list.add(map2);
					}
					break;
				}

			}
		}

		return list;
	}

	public static List<Map<String, Object>> compareTwoClassList(List<Object> class1, List<Object> class2, String commet) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		if(class1.size()!=class2.size())
		{
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("name", commet+"->"+class1.getClass().getName()+"条数不一致");
			map2.put("old", class1.size());
			map2.put("new", class2.size());
			list.add(map2);
			
			return list;
		}
		
		if(class1.size()==0)
		{
			return list;
		}
		
		if(isRoleObject(class1.get(0)))
		{
			String name = getClassKeyName(class1.get(0));
			for (Object obj1 : class1) {
				Object obj2 = getClassByKey(class2, name);
				
				list.addAll(compareTwoClass(obj1, obj2,commet));
			}
			return list;
		}
		
		for(int i=0;i<class1.size();i++)
		{
			list.addAll(compareTwoClass(class1.get(i), class2.get(i),commet));
		}
		return list;
	}
	
	public static String getClassKeyName(Object obj)
	{
		Class class1 = obj.getClass();
		Field[] fs1 = class1.getDeclaredFields();
		for (Field field : fs1) {
			if (field.isAnnotationPresent(Role.class)) {
				Role role = (Role) field.getAnnotation(Role.class);
				if (role.value() == "key") {
				}
					return field.getName();
				}
		}
		return "";
	}
	

	public static Object getClassByKey(List<Object> classList, Object key) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
	{
		for (Object object : classList) {
			Class class1 = object.getClass();
			Field[] fs1 = class1.getDeclaredFields();
			for (Field field : fs1) {
				if (field.isAnnotationPresent(Role.class)) {
					Role role = (Role) field.getAnnotation(Role.class);
					if (role.value() == "key") {
						
						String name = field.getName();
						name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写，方便构造get，set方法
				        Method m = object.getClass().getMethod("get" + name);
				    
				        Object value1 =  m.invoke(object);
				        if(value1.equals(key))
				        	return object;
						
					}
				}
			}
		}
		return null;
	}
	
	public static boolean isRoleObject(Object obj1) {
		Class class1 = obj1.getClass();
		Field[] fs1 = class1.getDeclaredFields();
		for (Field field : fs1) {
			if (field.isAnnotationPresent(Role.class)) {
				Role role = (Role) field.getAnnotation(Role.class);
				if (role.value() == "key") {
					return true;
				}
			}
		}

		return false;
	}      

	public static void main(String[] args) {
		try {
			UserInfo user1 = new UserInfo();
			UserInfo user2 = new UserInfo();

			user1.setId(1);
			user1.setName("testst1");
			user1.setSex(true);

			user2.setId(2);
			user2.setName("dfsgdfsgSDFG");
			user2.setSex(false);

			ReachPromitionVerify verify = new ReachPromitionVerify();

			List<Map<String, Object>> returnist = ReachPromitionVerify2.compareTwoClass(user1, user2,"test");

			for (Map<String, Object> map : returnist) {
				System.out.println(map.get("name") + " : " + map.get("old") + " | " + map.get("new"));
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
